<?php
/**
 * Created by PhpStorm.
 * User: Bobo
 * Date: 2017/4/12
 * Time: 15:28
 */

class DB {
    private $_conn = null;
    
    private $_errorMsg = '';
    
    private $_currQuery = ''; // 当前执行的sql语句
    
    function __construct() {
        $dbms='mysql';     //数据库类型
        $host='10.66.135.141'; //数据库主机名
        $dbName='appc';    //使用的数据库
        $user='zhiyou_orz';      //数据库连接用户名
        $pass='3819005Aa';          //对应的密码
        $dsn="$dbms:host=$host;dbname=$dbName";
        try{
            $this->_conn = $dbh = new PDO($dsn, $user, $pass); //初始化一个PDO对象
        } catch (PDOException $e) {
            $this->_errorMsg = $e->getMessage();
            return false;
        }
        return true;
    }
    
    public function getOne ($table,$map='',$field = '*'){
        $where = $this->_getWhere($map);
        $this->_currQuery = "SELECT $field from $table where $where LIMIT 1;";
        $rst = $this->_conn->query($this->_currQuery, PDO::FETCH_ASSOC);
        $one = $rst->fetch();
        return $one ? $one : array();
    }
    
    public function update ($table, $map='', $data) {
        if (empty($map)) return false;
        $set = '';
        $where = $this->_getWhere($map);
        $tmp = array();
        foreach ($data as $key=>$val) {
            if (is_array($val)) {
                if ($val[0] == 'exp' && count($val) == 2) {
                    $tmp[] = "`$key`=".addslashes($val[1]);
                }
            } else {
                $val = addslashes($val);
                $tmp[] = "`$key`='$val'";
            }
            
        }
        if (empty($tmp)) return false;
        $set = implode(',',$tmp);
        $this->_currQuery = "UPDATE $table SET $set WHERE $where;";
        $rst = $this->_conn->exec($this->_currQuery);
        if ($rst === false) {
            $this->_errorMsg = $this->_conn->errorInfo();
        }
        return $rst;
    }
    
    public function insert($table, $data){
        $values = '';
        $keys = '';
        $tmp_key = array();
        $tmp_val = array();
        foreach ($data as $key=>$val) {
            $tmp_key[] = "`$key`";
            $val = addslashes($val);
            $tmp_val[] = "'$val'";
        }
        $keys = implode(',',$tmp_key);
        $values = implode(',',$tmp_val);
        $this->_currQuery = "INSERT INTO $table ($keys) VALUES ($values);";
        $rst = $this->_conn->exec($this->_currQuery);
        if ($rst === false) {
            $this->_errorMsg = $this->_conn->errorInfo();
        }
        return $rst;
    }
    
    private function _getWhere($map=''){
        $where = '';
        if (is_string($map)) {
            $where = $map;
        } else if (is_array($map)) {
            $tmp = array();
            foreach ($map as $key=>$val) {
                $val = addslashes($val);
                $tmp[] = "`$key`='$val'";
            }
            $where = implode(' AND ',$tmp);
        }
        return $where;
    }
    
    public function close() {
        $this->_conn = null;
    }
    
    public function getError(){
        if(is_string($this->_errorMsg)) {
            return 'Query: '.$this->_currQuery.' \r\n '.$this->_errorMsg;
        } else {
            return 'Query: '.$this->_currQuery.' \r\n '.json_encode($this->_errorMsg);
        }
        
    }
}